﻿/*************************************************************
 * Software of blackcore
 *
 * CLRVersion:4.0.30319.296
 * MachineName:SPACE
 * Author:Andy Huang
 * Email:itblackhole@gmail.com
 * DateTime：5/30/2013 3:49:54 PM
 * Function：
 *************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace SobFS.Common
{
    public class CookieHelper
    {
        #region ******************************Private Property******************************
        #endregion ******************************Private Property******************************


        #region ******************************Public Property******************************
        #endregion ******************************Public Property******************************


        #region ******************************Structure******************************
        #endregion  ******************************Structure******************************


        #region ******************************Private Method******************************
        #endregion ******************************Private Method******************************


        #region ******************************Public Method******************************
        /// <summary>
        /// 写
        /// </summary>
        /// <param name="name">cookie对象名</param>        
        /// <param name="value">cookie中的key对应的value</param>
        public static void WriterCookie(string name, string value)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[name];
            if (cookie == null)
            {
                cookie = new HttpCookie(name);
            }
            cookie.Value = UrlEncode(value);
            HttpContext.Current.Response.AppendCookie(cookie);
        }

        /// <summary>
        /// 写
        /// </summary>
        /// <param name="name">cookie对象名</param>
        /// <param name="key">cookie中的key</param>
        /// <param name="value">cookie中的key对应的value</param>
        public static void WriterCookie(string name, string key, string value)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[name];
            if (cookie == null)
            {
                cookie = new HttpCookie(name);
            }
            cookie[key] = UrlEncode(value);
            HttpContext.Current.Response.AppendCookie(cookie);
        }

        /// <summary>
        /// 写
        /// </summary>
        /// <param name="name">cookie对象名</param>
        /// <param name="key">cookie中的key</param>
        /// <param name="value">cookie中的key对应的value</param>
        /// <param name="expires">单位为分钟</param>
        public static void WriterCookie(string name, string key, string value, int expires)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[name];
            if (cookie == null)
            {
                cookie = new HttpCookie(name);
            }
            cookie[key] = UrlEncode(value);
            cookie.Expires = DateTime.Now.AddMinutes(expires);
            HttpContext.Current.Response.AppendCookie(cookie);
        }

        /// <summary>
        /// 写
        /// </summary>
        /// <param name="name">cookie对象名</param>        
        /// <param name="value">cookie中的key对应的value</param>
        /// <param name="expires">单位为分钟</param>
        public static void WriterCookie(string name, string value, int expires)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[name];
            if (cookie == null)
            {
                cookie = new HttpCookie(name);
            }
            cookie.Value = UrlEncode(value);
            cookie.Expires = DateTime.Now.AddMinutes(expires);
            HttpContext.Current.Response.AppendCookie(cookie);
        }

        /// <summary>
        /// 读
        /// </summary>
        /// <param name="name">cookie对象名</param> 
        /// <returns>cookie值</returns>
        public static string ReaderCookie(string name)
        {
            if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[name] != null)
            {
                return UrlDecode(HttpContext.Current.Request.Cookies[name].Value.ToString());
            }
            return "";
        }

        /// <summary>
        /// 读
        /// </summary>
        /// <param name="name">cookie对象名</param>
        /// <param name="key">cookie中的key</param>
        /// <returns>cookie值</returns>
        public static string ReaderCookie(string name, string key)
        {
            if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[name] != null && HttpContext.Current.Request.Cookies[name][key] != null)
            {
                return UrlDecode(HttpContext.Current.Request.Cookies[name][key].ToString());
            }
            return "";
        }


        /// <summary>
        /// URL字符编码
        /// </summary>
        public static string UrlEncode(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return "";
            }
            str = str.Replace("'", "");
            return HttpContext.Current.Server.UrlEncode(str);
        }

        /// <summary>
        /// URL字符解码
        /// </summary>
        public static string UrlDecode(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return "";
            }
            return HttpContext.Current.Server.UrlDecode(str);
        }
        #endregion ******************************Public Method******************************
    }
}
